layui.use(['form', 'layedit', 'laydate'], function(){
    var form = layui.form
        ,layer = layui.layer
        ,layedit = layui.layedit
        ,$ = layui.$
        ,laydate = layui.laydate;
    //创建一个编辑器
    var editIndex = layedit.build('LAY_demo_editor');
    //自定义验证规则
    form.verify({
        title: function(value){
            if(value.length < 5){
                return '标题至少得5个字符啊';
            }
        }
        ,pass: [
            /^[\S]{6,12}$/
            ,'密码必须6到12位，且不能出现空格'
        ]
        ,content: function(value){
            layedit.sync(editIndex);
        }
    });


    //监听提交
    form.on('submit(demo1)', function(data){
        $.ajax({
            url:serverUrl +"/admin/login",
            data:data.field,
            type:'post',
            dataType:'json',
            success:function (result) {
                if(result.code=='200') {
                    var username = result.data.username;
                    sessionStorage.setItem("loginUser", username);
                    location.href='admin.html';
                } else {
                    layer.msg(result.msg);
                }

            },
            error: function (xhr) {
                console.log(xhr);
                layer.msg('出错了');
            }
        })
        return false;
    });

});